package com.sxit.tdf.server.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxit.tdf.server.entity.User;
import com.sxit.tdf.server.entity.bo.*;
import com.sxit.tdf.server.entity.vo.InsertUserInfoVo;
import com.sxit.tdf.server.entity.vo.LoginByUserNameAndPasswordFrom;
import com.sxit.tdf.server.entity.vo.SiftUserInfoVo;
import com.sxit.tdf.server.exception.TdfException;
import com.sxit.tdf.server.mapper.UserMapper;
import com.sxit.tdf.server.service.IUserService;
import com.sxit.tdf.server.util.EmailSendUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * 用户表 服务实现类
 *
 * @author 橙子高🍊
 * @since 2022-02-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

  @Autowired private EmailSendUtil emailSendUtil;

  @Override
  public List<UserInfoBo> searchAllUser() {
    return baseMapper.searchAllUser();
  }

  @Override
  public String loginByUserNameAndPassword(LoginByUserNameAndPasswordFrom from) {

    String userName = from.getUsername();
    // 1.判断账户名是否存在
    boolean exsitUsername = baseMapper.isExistUsername(userName);
    System.out.println(exsitUsername);
    System.out.println(userName);
    if (exsitUsername) {
      // 2.判断密码是否一致
      UserIdAndPassword userIdAndPassword = baseMapper.searchPasswordByUsername(userName);

      return userIdAndPassword.getPassword().equals(from.getPassword())
          ? userIdAndPassword.getId()
          : null;

    } else {

      throw new TdfException("该用户名不存在");
    }
  }

  @Override
  public Set<String> getUserPermissions(String userId) {
    return baseMapper.searchUserPermission(userId);
  }

  @Override
  public UserInfoBo getUserInfo(String userId) {
    return baseMapper.searchUserInfo(userId);
  }

  @Override
  public void deleteUserByIds(List<String> ids) {

    baseMapper.deleteBatchIds(ids);
  }

  @Override
  public void createRandomUserInfo() {

    User user = new User();
    user.setNum(RandomUtil.randomNumbers(9));
    user.setPortrait(
        "https://img03.sogoucdn.com/v2/thumb/crop/xy/ai/x/0/y/0/w/120/h/80/iw/90/ih/60/t/0/ir/3/retype_exclude_gif/ext/auto/q/75?t=2&appid=200997&url=https%3A%2F%2Fimg-blog.csdnimg.cn%2F50c826eb04c74b5786c5ad95262bff5d.png&sign=eb18a2afe5ecffbbbc707b7c702eb4c2");
    String username = RandomUtil.randomString(8);
    String name =
        String.valueOf(
            RandomUtil.randomChinese() + RandomUtil.randomChinese() + RandomUtil.randomChinese());
    user.setUsername(username);
    user.setPhone(RandomUtil.randomNumbers(11));
    user.setName(name);
    user.setSex(String.valueOf(RandomUtil.randomInt(1, 2)));
    user.setEmail(username + "@qq.com");
    user.setStatu(RandomUtil.randomInt(1, 4));
    user.setRoot(false);
    user.setCreateTime(LocalDateTime.now());
    user.setUpdateTime(LocalDateTime.now());
    user.setDeptId(RandomUtil.randomInt(1, 3));
    user.setRoleId(RandomUtil.randomInt(1, 6));
    user.setPassword(username);

    baseMapper.insert(user);
  }

  @Override
  public IPage<UserInfoBo> getUserInfoBySift(SiftUserInfoVo siftUserInfoVo) {

    HashMap hashMap = JSONUtil.parse(siftUserInfoVo).toBean(HashMap.class);

    Page<UserInfoBo> page =
        new Page<UserInfoBo>(siftUserInfoVo.getCurrent(), siftUserInfoVo.getPageSize());
    return baseMapper.searchAllUserBySift(page, hashMap);
  }

  @Override
  public int insertUserInfo(InsertUserInfoVo insertUserInfoVo) {

    String num = insertUserInfoVo.getNum();

    User user = new User();

    user.setId(UUID.randomUUID().toString());
    user.setRoleId(insertUserInfoVo.getRole());
    user.setNum(insertUserInfoVo.getNum());
    user.setPortrait(
        "https://sxit-gc.oss-cn-beijing.aliyuncs.com/tdf-bms-file/%E7%94%B7%E5%A4%B4%E5%83%8F.png");

    user.setUsername("sxit" + num);
    user.setPhone(insertUserInfoVo.getPhone());
    user.setName(insertUserInfoVo.getName());
    user.setSex(insertUserInfoVo.getSex() == 1 ? "男" : "女");
    user.setEmail(insertUserInfoVo.getEmail());
    user.setStatu(insertUserInfoVo.getStatu());
    user.setRoot(false);
    user.setProfessionId(insertUserInfoVo.getProfessional());
    user.setDeptId(insertUserInfoVo.getDept());
    user.setPassword(num);

    HashMap hashMap = JSONUtil.parse(user).toBean(HashMap.class);

    baseMapper.insertUserInfo(hashMap);

    //
    if (insertUserInfoVo.isNotification()) {

      String msg = "你好！欢迎使用教学资料关系系统，您的账号已经注册成功，用户名为sxit" + user.getNum() + "密码为学号或工作号.";
      emailSendUtil.SendMsg(msg, user.getEmail(), "用户注册成功");
    }

    return 1;
  }

  @Override
  public InsertUserInfoVo searchUserInfoInInsert(String userId) {
    return baseMapper.searchUserInfoInInsert(userId);
  }

  @Override
  public int updateUserInfo(InsertUserInfoVo insertUserInfoVo) {
    System.out.println("---2");
    String num = insertUserInfoVo.getNum();

    User user = new User();
    user.setId(insertUserInfoVo.getId());
    user.setRoleId(insertUserInfoVo.getRole());
    user.setNum(insertUserInfoVo.getNum());
    user.setPhone(insertUserInfoVo.getPhone());
    user.setName(insertUserInfoVo.getName());
    user.setSex(insertUserInfoVo.getSex() == 1 ? "男" : "女");
    user.setEmail(insertUserInfoVo.getEmail());
    user.setStatu(insertUserInfoVo.getStatu());
    user.setProfessionId(insertUserInfoVo.getProfessional());
    user.setUpdateTime(LocalDateTime.now());
    user.setDeptId(insertUserInfoVo.getDept());

    return baseMapper.updateById(user);
  }

  @Override
  public List<SimpleClassesIntoUserInfoBo> getSimpleClassesIntoUserInfoBo(int id) {
    return baseMapper.getSimpleClassesIntoUserInfoBo(id);
  }

  @Override
  public List<SimpleUserInfoToSift> getSimpleUserInfoToSiftList() {
    return baseMapper.getSimpleUserInfoToSiftList();
  }

  @Override
  public List<SimpleTeacherList> getSimpleTeacherList() {
    return baseMapper.getSimpleTeacherList();
  }

  @Override
  public List<SimpleUserInfo> getSimpleUserInfo() {

    return baseMapper.getSimpleUserInfo();
  }

  @Override
  public void modifyPassword(String password, String id, String userName) {
    baseMapper.modifyPassword(password, id, userName);
  }
}
